package exam.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/24 14:35
 * @since JDK 1.8
 *
 * 校验工具类
 */
public class VerifyUtils {

    private static final String IS_MAIl="^([a-z0-9A-Z]+[-|\\\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\\\.)+[a-zA-Z]{2,}$";

    private static final String IS_MOBILE="  ^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";

    private static final String IS_INTEGER="^[1-9]\\d*$";

    private static final String IS_NUM="^[0-9]*$";

    /**
     * 校验数字
     * @param str
     * @return
     */
    public static boolean isNum(String str){
        return verifyPattern(str,IS_NUM);
    }


    /**
     * 校验非0的正整数
     * @param str
     * @return
     */
    public static boolean isInteger(String str){
        return verifyPattern(str,IS_INTEGER);
    }

    /**
     * 校验是否是邮箱
     * @param str
     * @return
     */
    public static boolean isMail(String str){
        return verifyPattern(str,IS_MAIl);
    }

    /**
     * 校验是否是手机号
     * @param str
     * @return
     */
    public static boolean isMobile(String str){
        return verifyPattern(str,IS_MOBILE);
    }


    /**
     * 校验工具类
     * @param str
     * @param regEx1
     * @return
     */
    private static boolean verifyPattern(String str,String regEx1) {
        boolean flag = false;
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(str);
        if(m.matches()){
            flag = true;
        }
        return flag;
    }
}
